package kuba.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.util.logging.Level;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import kuba.bean.Author;
import kuba.bean.CD;
import kuba.common.BeanLogger;
import kuba.common.Messages;
import kuba.configuration.ApplicationConfiguration;

import org.eclipse.wb.swing.FocusTraversalOnArray;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class MainWindow extends JFrame{
	public MainWindow() {
		//init();
	}

    private static final long serialVersionUID = 1L;
    
    private static ApplicationContext context;
    
    JTextField txtAuthorName;
    JTextField txtAuthorLastName;
    JTabbedPane tabbedPane;
    JButton btnAuthorAdd;
    JButton btnAuthorDelete;
    JButton btnAuthorUpdate;
    JLabel lblName;
    JLabel lblLastName;
    JList<Author> listAuthor;
    JButton btnAuthorGetAll;
    JLabel lblAuthors;
    JButton btnAuthorCd;
    JTextField txtCdGenere;
    JTextField txtCdTitle;
    JLabel lblTitle;
    JLabel lblGenere;
    JButton btnCdAdd;
    JButton btnCdUpdate;
    JButton btnCdDelete;
    JButton btnCdGetAll;
    JLabel lblCds;
    JList<CD> listCd;
    JComboBox<Author> comboCdAuthor;
    JLabel lblAuthor;
    JScrollPane scrollPane;
    JScrollPane scrollPane1;

    public static void main(String[] args) {
        
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(ClassNotFoundException e) {
            BeanLogger.getLOGGER().log(Level.WARNING,e.toString());
        } catch (InstantiationException e) {
            BeanLogger.getLOGGER().log(Level.WARNING,e.toString());
        } catch (IllegalAccessException e) {
            BeanLogger.getLOGGER().log(Level.WARNING,e.toString());
        } catch (UnsupportedLookAndFeelException e) {
            BeanLogger.getLOGGER().log(Level.WARNING,e.toString());
        }
    
        System.setProperty("spring.profiles.active", "mysql"); //$NON-NLS-1$ //$NON-NLS-2$
        context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
        
        
        EventQueue.invokeLater(new Runnable(){
            public void run(){
                try{
                    MainWindow mainWindow = context.getBean(MainWindow.class);
                    mainWindow.init();
                    mainWindow.setLocationRelativeTo(null);
                    mainWindow.setVisible(true);


                }catch(Exception e){
                    BeanLogger.getLOGGER().log(Level.WARNING,e.toString());
                }
            }
        });
        
    }
   
    
    private  void init() {
        this.setBounds(0, 0, 400, 300);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        
        getContentPane().add(tabbedPane, BorderLayout.CENTER);
        
        
        authorPanelInit();
        cdPanelInit();
        tabbedPane.addChangeListener(context.getBean(ButtonListener.class));
        btnAuthorGetAll.doClick();

        
    }

    private void authorPanelInit() {
    

        JPanel panelAuthor = new JPanel();
        tabbedPane.addTab(Messages.getString("MainWindow.author"), null, panelAuthor, null); //$NON-NLS-1$
        panelAuthor.setLayout(null);

        txtAuthorName = new JTextField();
        txtAuthorName.setBounds(10, 31, 86, 20);
        panelAuthor.add(txtAuthorName);
        txtAuthorName.setColumns(10);

        txtAuthorLastName = new JTextField();
        txtAuthorLastName.setBounds(133, 31, 86, 20);
        panelAuthor.add(txtAuthorLastName);
        txtAuthorLastName.setColumns(10);

        btnAuthorAdd = new JButton(Messages.getString("MainWindow.add")); //$NON-NLS-1$
    
        btnAuthorAdd.setBounds(10, 62, 83, 23);
        btnAuthorAdd.addActionListener(context.getBean(ButtonListener.class));
        panelAuthor.add(btnAuthorAdd);

        btnAuthorDelete = new JButton(Messages.getString("MainWindow.delete")); //$NON-NLS-1$
        btnAuthorDelete.setBounds(296, 62, 83, 23);
        btnAuthorDelete.addActionListener(context.getBean(ButtonListener.class));
        panelAuthor.add(btnAuthorDelete);

        btnAuthorUpdate = new JButton(Messages.getString("MainWindow.update")); //$NON-NLS-1$
        btnAuthorUpdate.setBounds(103, 62, 83, 23);
        btnAuthorUpdate.addActionListener(context.getBean(ButtonListener.class));
        panelAuthor.add(btnAuthorUpdate);
        
        lblName = new JLabel(Messages.getString("MainWindow.name")); //$NON-NLS-1$
        lblName.setHorizontalAlignment(SwingConstants.CENTER);
        lblName.setBounds(10, 6, 86, 14);
        panelAuthor.add(lblName);
        
        lblLastName = new JLabel(Messages.getString("MainWindow.lastName")); //$NON-NLS-1$
        lblLastName.setHorizontalAlignment(SwingConstants.CENTER);
        lblLastName.setBounds(133, 6, 84, 14);
        panelAuthor.add(lblLastName);
        
        scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 152, 369, 81);
        panelAuthor.add(scrollPane);
        
        listAuthor = new JList<Author>();
        scrollPane.setViewportView(listAuthor);
        listAuthor.addListSelectionListener(context.getBean(ButtonListener.class));
        
        btnAuthorGetAll = new JButton(Messages.getString("MainWindow.getAll")); //$NON-NLS-1$
        btnAuthorGetAll.setBounds(10, 96, 83, 23);
        btnAuthorGetAll.addActionListener(context.getBean(ButtonListener.class));
        panelAuthor.add(btnAuthorGetAll);
        
        lblAuthors = new JLabel(Messages.getString("MainWindow.authors")); //$NON-NLS-1$
        lblAuthors.setBounds(171, 131, 46, 14);
        panelAuthor.add(lblAuthors);
        
        btnAuthorCd = new JButton(Messages.getString("MainWindow.getCDs")); //$NON-NLS-1$
        btnAuthorCd.setBounds(103, 96, 83, 23);
        btnAuthorCd.addActionListener(context.getBean(ActionListener.class));
        panelAuthor.add(btnAuthorCd);
        getContentPane().setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{tabbedPane}));
    }
    
    private void cdPanelInit() {
        JPanel panelCd = new JPanel();
        tabbedPane.addTab(Messages.getString("MainWindow.CD"), null, panelCd, null); //$NON-NLS-1$
        panelCd.setLayout(null);
        
        txtCdGenere = new JTextField();
        txtCdGenere.setBounds(133, 34, 86, 20);
        panelCd.add(txtCdGenere);
        txtCdGenere.setColumns(10);
        
        txtCdTitle = new JTextField();
        txtCdTitle.setBounds(10, 34, 86, 20);
        panelCd.add(txtCdTitle);
        txtCdTitle.setColumns(10);
        
        lblTitle = new JLabel(Messages.getString("MainWindow.title")); //$NON-NLS-1$
        lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
        lblTitle.setBounds(10, 11, 86, 14);
        panelCd.add(lblTitle);
        
        lblGenere = new JLabel(Messages.getString("MainWindow.genere")); //$NON-NLS-1$
        lblGenere.setHorizontalAlignment(SwingConstants.CENTER);
        lblGenere.setBounds(133, 11, 86, 14);
        panelCd.add(lblGenere);
        
        btnCdAdd = new JButton(Messages.getString("MainWindow.add")); //$NON-NLS-1$
        btnCdAdd.setBounds(10, 65, 83, 23);
        btnCdAdd.addActionListener(context.getBean(ButtonListener.class));
        panelCd.add(btnCdAdd);
        
        btnCdUpdate = new JButton(Messages.getString("MainWindow.update")); //$NON-NLS-1$
        btnCdUpdate.setBounds(103, 65, 83, 23);
        btnCdUpdate.addActionListener(context.getBean(ButtonListener.class));
        panelCd.add(btnCdUpdate);
        
        btnCdDelete = new JButton(Messages.getString("MainWindow.delete")); //$NON-NLS-1$
        btnCdDelete.setBounds(296, 65, 83, 23);
        btnCdDelete.addActionListener(context.getBean(ButtonListener.class));
        panelCd.add(btnCdDelete);
        
        btnCdGetAll = new JButton(Messages.getString("MainWindow.getAll")); //$NON-NLS-1$
        btnCdGetAll.setBounds(10, 99, 83, 23);
        btnCdGetAll.addActionListener(context.getBean(ButtonListener.class));
        panelCd.add(btnCdGetAll);
        
        lblCds = new JLabel(Messages.getString("MainWindow.cds")); //$NON-NLS-1$
        lblCds.setBounds(171, 134, 46, 14);
        panelCd.add(lblCds);
        
        scrollPane1 = new JScrollPane();
        scrollPane1.setBounds(10, 155, 369, 81);
        panelCd.add(scrollPane1);
        
        listCd = new JList<CD>();
        scrollPane1.setViewportView(listCd);
        listCd.addListSelectionListener(context.getBean(ButtonListener.class));
        
        comboCdAuthor = new JComboBox<Author>();
        comboCdAuthor.setBounds(261, 34, 104, 20);
        panelCd.add(comboCdAuthor);
        
        lblAuthor = new JLabel(Messages.getString("MainWindow.author")); //$NON-NLS-1$
        lblAuthor.setHorizontalAlignment(SwingConstants.CENTER);
        lblAuthor.setBounds(261, 11, 104, 14);
        panelCd.add(lblAuthor);
    }
}
